What is mumath?
The mumath npm package provides a collection of mathematical utilities for common operations such as rounding, clamping, and interpolation. It is designed to be lightweight and easy to use for basic mathematical tasks.
What are mumath's main functionalities?
Rounding
The rounding feature allows you to round numbers to the nearest integer. The code sample demonstrates rounding 4.6 up to 5 and 4.4 down to 4.
const mumath = require('mumath');
console.log(mumath.round(4.6)); // 5
console.log(mumath.round(4.4)); // 4
Clamping
The clamping feature restricts a number to be within a specified range. The code sample shows clamping 10 to the maximum value of 5 and -1 to the minimum value of 0.
const mumath = require('mumath');
console.log(mumath.clamp(10, 0, 5)); // 5
console.log(mumath.clamp(-1, 0, 5)); // 0
Interpolation
The interpolation feature allows you to find a value at a specific point between two numbers. The code sample demonstrates finding the midpoint (0.5) between 0 and 10, which is 5.
const mumath = require('mumath');
console.log(mumath.lerp(0, 10, 0.5)); // 5
Other packages similar to mumath
mathjs
Math.js is an extensive math library for JavaScript and Node.js. It provides a wide range of mathematical functions and supports complex numbers, matrices, and units. Compared to mumath, math.js offers a broader set of features and is suitable for more complex mathematical operations.
lodash
Lodash is a utility library that provides functions for common programming tasks, including mathematical operations. It includes methods for rounding, clamping, and interpolation, similar to mumath. However, Lodash is a more comprehensive library that covers a wide range of utilities beyond just math.
numeral
Numeral.js is a library for formatting and manipulating numbers. While it focuses more on number formatting and parsing, it also provides some basic mathematical operations. Compared to mumath, Numeral.js is more specialized in number formatting rather than general mathematical utilities.
μMath
Set of practical math utils to shorten code.
$ npm install mumath
var round = require('mumath/round');
round(123.32, .5);
API
round(value, step?)
Rounds value to optional step
.
round(0.3, .5)
→ .5
len(a, b)
Return length of a vector.
precision(value)
Get precision from float:
1.1 → 1, 1234 → 0, .1234 → 4
clamp(value, left, right)
Return value clamped by left/right limits (or vice-versa).
lerp(x, y, ratio)
Return value interpolated between x and y.
within(value, left, right)
Whether element is between left & right, including.
mod(value, min?, max)
An enhanced mod-loop — loops value within a frame.
closest(value, list)
Get closest value out of a set.
scale(value, list)
Get first scale out of a list of basic scales, aligned to the power. E. g.
step(.37, [1, 2, 5])
→ .5
step(456, [1, 2])
→ 1000
Similar to closest, but takes all possible powers of scales.
order(value)
Get order of magnitude for a number.
order(123) → 100; order(-0.0003) → 0.0001;
isMultiple(a, b, eps?)
Same as a % b === 0
, but with precision check.
Related